import {
	asyncRouterMap
} from '@/router'

/**
 *
 * @param {*} router
 * @param {*} authInfo
 */
function checkAuth(router, authInfo) {
	// return console.log(authInfo);
	// 判断当前路由在权限数组中是否存在
	if (router.meta) {
		const metaInfo = router.meta
		// console.log(authInfo);
		if (!metaInfo.requiresAuth) {
			return true
		} else {
			if (metaInfo.index == 0) {
				return !!authInfo[metaInfo.type]
			} else if (metaInfo.index == 1) {
				if (authInfo[metaInfo.type]) {
					return !!authInfo[metaInfo.type][metaInfo.subType]
				}
				return false
			} else {
				var typeAuth = authInfo[metaInfo.type]
				for (let index = 0; index < metaInfo.subType.length; index++) {
					const field = metaInfo.subType[index]
					typeAuth = typeAuth[field]
					if (typeAuth && metaInfo.subType.length - 1 == index) {
						return true
					} else if (!typeAuth) {
						return false
					}
				}
			}
		}
	}
	return true
}

/**
 *
 * @param {*} routers
 * @param {*} authInfo
 */
const filterAsyncRouter = function(routers, authInfo) {
	const res = []
	routers.forEach(router => {
		const tmp = {
			...router
		}
		if (checkAuth(tmp, authInfo)) {
			if (tmp.children) {
				tmp.children = filterAsyncRouter(tmp.children, authInfo)
			}
			res.push(tmp)
		}
	})
	return res
}

const permission = {
	state: {
		addRouters: [],
		indexRouters:{
			name: 'index',
			children: []
		},
		customerRouters:{
			name: 'customer',
			children: []
		},
		lawcaseRouters:{
			name: 'lawcase',
			children: []
		},
		contractRouters:{
			name: 'contract',
			children: []
		},
		financeRouters:{
			name: 'finance',
			children: []
		},
		settingRouters: {
			name: 'setting',
			children: []
		}
	},
	mutations: {
		SET_ROUTERS: (state, routers) => {
			state.addRouters = routers
			for (let index = 0; index < routers.length; index++) {
				const element = routers[index]
				if (element.name == 'setting') {
					state.settingRouters = element
				}else if (element.name == 'customer') {
				  state.customerRouters = element
				}else if (element.name == 'finance') {
				  state.financeRouters = element
				}else if (element.name == 'lawcase') {
				  state.lawcaseRouters = element
				}else if (element.name == 'contract') {
				  state.contractRouters = element
				}else if (element.name == 'index') {
				  state.indexRouters = element
				}
			}
		}
	},
	actions: {
		GenerateRoutes({
			commit
		}, data) {
			return new Promise(resolve => {
				// asyncRouterMap原路由 data权限数据
				const accessedRouters = filterAsyncRouter(asyncRouterMap, data)
				// console.log(accessedRouters);
				let redirect = ''
				for (let index = 0; index < accessedRouters.length; index++) {
					const element = accessedRouters[index]
					if (element.children && element.children.length > 0) {
						element.redirect = element.path + '/' + element.children[0].path
					}

					// 获取跳转
					if (element.redirect && !redirect) {
						redirect = element.redirect
					}
				}
				if (redirect) {
					accessedRouters.push({
						path: '/',
						redirect: redirect,
						hidden: true
					})
				}
				commit('SET_ROUTERS', accessedRouters)
				resolve()
			})
		}
	}
}

export default permission
